#!/bin/sh

die () { echo $@ ; cleanup ; exit 1; }

cleanup() {
  unset -f nvm_download
}

. ../../../nvm.sh

# sample output at the time the test was written
nvm_download() {
  echo 'foo "v0.10.0'
  echo 'foo "v0.10.0'
  echo 'foo "v0.10.1'
  echo 'foo "v0.10.1'
  echo 'foo "v0.10.10'
  echo 'foo "v0.10.10'
  echo 'foo "v0.10.11'
  echo 'foo "v0.10.11'
  echo 'foo "v0.10.12'
  echo 'foo "v0.10.12'
  echo 'foo "v0.10.13'
  echo 'foo "v0.10.13'
  echo 'foo "v0.10.14'
  echo 'foo "v0.10.14'
  echo 'foo "v0.10.15'
  echo 'foo "v0.10.15'
  echo 'foo "v0.10.16'
  echo 'foo "v0.10.16'
  echo 'foo "v0.10.16'
  echo 'foo "v0.10.16'
  echo 'foo "v0.10.17'
  echo 'foo "v0.10.17'
  echo 'foo "v0.10.18'
  echo 'foo "v0.10.18'
  echo 'foo "v0.10.19'
  echo 'foo "v0.10.19'
  echo 'foo "v0.10.2'
  echo 'foo "v0.10.2'
  echo 'foo "v0.10.20'
  echo 'foo "v0.10.20'
  echo 'foo "v0.10.21'
  echo 'foo "v0.10.21'
  echo 'foo "v0.10.22'
  echo 'foo "v0.10.22'
  echo 'foo "v0.10.23'
  echo 'foo "v0.10.23'
  echo 'foo "v0.10.24'
  echo 'foo "v0.10.24'
  echo 'foo "v0.10.25'
  echo 'foo "v0.10.25'
  echo 'foo "v0.10.26'
  echo 'foo "v0.10.26'
  echo 'foo "v0.10.27'
  echo 'foo "v0.10.27'
  echo 'foo "v0.10.28'
  echo 'foo "v0.10.28'
  echo 'foo "v0.10.29'
  echo 'foo "v0.10.29'
  echo 'foo "v0.10.3'
  echo 'foo "v0.10.3'
  echo 'foo "v0.10.30'
  echo 'foo "v0.10.30'
  echo 'foo "v0.10.31'
  echo 'foo "v0.10.31'
  echo 'foo "v0.10.32'
  echo 'foo "v0.10.32'
  echo 'foo "v0.10.4'
  echo 'foo "v0.10.4'
  echo 'foo "v0.10.5'
  echo 'foo "v0.10.5'
  echo 'foo "v0.10.6'
  echo 'foo "v0.10.6'
  echo 'foo "v0.10.7'
  echo 'foo "v0.10.7'
  echo 'foo "v0.10.8'
  echo 'foo "v0.10.8'
  echo 'foo "v0.10.9'
  echo 'foo "v0.10.9'
  echo 'foo "v0.11.0'
  echo 'foo "v0.11.0'
  echo 'foo "v0.11.1'
  echo 'foo "v0.11.1'
  echo 'foo "v0.11.10'
  echo 'foo "v0.11.10'
  echo 'foo "v0.11.11'
  echo 'foo "v0.11.11'
  echo 'foo "v0.11.12'
  echo 'foo "v0.11.12'
  echo 'foo "v0.11.13'
  echo 'foo "v0.11.13'
  echo 'foo "v0.11.14'
  echo 'foo "v0.11.14'
  echo 'foo "v0.11.2'
  echo 'foo "v0.11.2'
  echo 'foo "v0.11.3'
  echo 'foo "v0.11.3'
  echo 'foo "v0.11.4'
  echo 'foo "v0.11.4'
  echo 'foo "v0.11.5'
  echo 'foo "v0.11.5'
  echo 'foo "v0.11.6'
  echo 'foo "v0.11.6'
  echo 'foo "v0.11.7'
  echo 'foo "v0.11.7'
  echo 'foo "v0.11.8'
  echo 'foo "v0.11.8'
  echo 'foo "v0.11.9'
  echo 'foo "v0.11.9'
  echo 'foo "v0.5.1'
  echo 'foo "v0.5.1'
  echo 'foo "v0.5.10'
  echo 'foo "v0.5.10'
  echo 'foo "v0.5.2'
  echo 'foo "v0.5.2'
  echo 'foo "v0.5.3'
  echo 'foo "v0.5.3'
  echo 'foo "v0.5.4'
  echo 'foo "v0.5.4'
  echo 'foo "v0.5.5'
  echo 'foo "v0.5.5'
  echo 'foo "v0.5.6'
  echo 'foo "v0.5.6'
  echo 'foo "v0.5.7'
  echo 'foo "v0.5.7'
  echo 'foo "v0.5.8'
  echo 'foo "v0.5.8'
  echo 'foo "v0.5.9'
  echo 'foo "v0.5.9'
  echo 'foo "v0.6.0'
  echo 'foo "v0.6.0'
  echo 'foo "v0.6.1'
  echo 'foo "v0.6.1'
  echo 'foo "v0.6.10'
  echo 'foo "v0.6.10'
  echo 'foo "v0.6.11'
  echo 'foo "v0.6.11'
  echo 'foo "v0.6.12'
  echo 'foo "v0.6.12'
  echo 'foo "v0.6.13'
  echo 'foo "v0.6.13'
  echo 'foo "v0.6.14'
  echo 'foo "v0.6.14'
  echo 'foo "v0.6.15'
  echo 'foo "v0.6.15'
  echo 'foo "v0.6.16'
  echo 'foo "v0.6.16'
  echo 'foo "v0.6.17'
  echo 'foo "v0.6.17'
  echo 'foo "v0.6.18'
  echo 'foo "v0.6.18'
  echo 'foo "v0.6.19'
  echo 'foo "v0.6.19'
  echo 'foo "v0.6.2'
  echo 'foo "v0.6.2'
  echo 'foo "v0.6.20'
  echo 'foo "v0.6.20'
  echo 'foo "v0.6.21'
  echo 'foo "v0.6.21'
  echo 'foo "v0.6.3'
  echo 'foo "v0.6.3'
  echo 'foo "v0.6.4'
  echo 'foo "v0.6.4'
  echo 'foo "v0.6.5'
  echo 'foo "v0.6.5'
  echo 'foo "v0.6.6'
  echo 'foo "v0.6.6'
  echo 'foo "v0.6.7'
  echo 'foo "v0.6.7'
  echo 'foo "v0.6.8'
  echo 'foo "v0.6.8'
  echo 'foo "v0.6.9'
  echo 'foo "v0.6.9'
  echo 'foo "v0.7.0'
  echo 'foo "v0.7.0'
  echo 'foo "v0.7.1'
  echo 'foo "v0.7.1'
  echo 'foo "v0.7.10'
  echo 'foo "v0.7.10'
  echo 'foo "v0.7.11'
  echo 'foo "v0.7.11'
  echo 'foo "v0.7.12'
  echo 'foo "v0.7.12'
  echo 'foo "v0.7.2'
  echo 'foo "v0.7.2'
  echo 'foo "v0.7.3'
  echo 'foo "v0.7.3'
  echo 'foo "v0.7.4'
  echo 'foo "v0.7.4'
  echo 'foo "v0.7.5'
  echo 'foo "v0.7.5'
  echo 'foo "v0.7.6'
  echo 'foo "v0.7.6'
  echo 'foo "v0.7.7'
  echo 'foo "v0.7.7'
  echo 'foo "v0.7.8'
  echo 'foo "v0.7.8'
  echo 'foo "v0.7.9'
  echo 'foo "v0.7.9'
  echo 'foo "v0.8.0'
  echo 'foo "v0.8.0'
  echo 'foo "v0.8.1'
  echo 'foo "v0.8.1'
  echo 'foo "v0.8.10'
  echo 'foo "v0.8.10'
  echo 'foo "v0.8.11'
  echo 'foo "v0.8.11'
  echo 'foo "v0.8.12'
  echo 'foo "v0.8.12'
  echo 'foo "v0.8.13'
  echo 'foo "v0.8.13'
  echo 'foo "v0.8.14'
  echo 'foo "v0.8.14'
  echo 'foo "v0.8.15'
  echo 'foo "v0.8.15'
  echo 'foo "v0.8.16'
  echo 'foo "v0.8.16'
  echo 'foo "v0.8.17'
  echo 'foo "v0.8.17'
  echo 'foo "v0.8.18'
  echo 'foo "v0.8.18'
  echo 'foo "v0.8.19'
  echo 'foo "v0.8.19'
  echo 'foo "v0.8.2'
  echo 'foo "v0.8.2'
  echo 'foo "v0.8.20'
  echo 'foo "v0.8.20'
  echo 'foo "v0.8.21'
  echo 'foo "v0.8.21'
  echo 'foo "v0.8.22'
  echo 'foo "v0.8.22'
  echo 'foo "v0.8.23'
  echo 'foo "v0.8.23'
  echo 'foo "v0.8.24'
  echo 'foo "v0.8.24'
  echo 'foo "v0.8.25'
  echo 'foo "v0.8.25'
  echo 'foo "v0.8.26'
  echo 'foo "v0.8.26'
  echo 'foo "v0.8.27'
  echo 'foo "v0.8.27'
  echo 'foo "v0.8.28'
  echo 'foo "v0.8.28'
  echo 'foo "v0.8.3'
  echo 'foo "v0.8.3'
  echo 'foo "v0.8.4'
  echo 'foo "v0.8.4'
  echo 'foo "v0.8.5'
  echo 'foo "v0.8.5'
  echo 'foo "v0.8.6'
  echo 'foo "v0.8.6'
  echo 'foo "v0.8.7'
  echo 'foo "v0.8.7'
  echo 'foo "v0.8.8'
  echo 'foo "v0.8.8'
  echo 'foo "v0.8.9'
  echo 'foo "v0.8.9'
  echo 'foo "v0.9.0'
  echo 'foo "v0.9.0'
  echo 'foo "v0.9.1'
  echo 'foo "v0.9.1'
  echo 'foo "v0.9.10'
  echo 'foo "v0.9.10'
  echo 'foo "v0.9.11'
  echo 'foo "v0.9.11'
  echo 'foo "v0.9.12'
  echo 'foo "v0.9.12'
  echo 'foo "v0.9.2'
  echo 'foo "v0.9.2'
  echo 'foo "v0.9.3'
  echo 'foo "v0.9.3'
  echo 'foo "v0.9.4'
  echo 'foo "v0.9.4'
  echo 'foo "v0.9.5'
  echo 'foo "v0.9.5'
  echo 'foo "v0.9.6'
  echo 'foo "v0.9.6'
  echo 'foo "v0.9.7'
  echo 'foo "v0.9.7'
  echo 'foo "v0.9.8'
  echo 'foo "v0.9.8'
  echo 'foo "v0.9.9'
  echo 'foo "v0.9.9'
  echo 'foo "v0.1.100'
  echo 'foo "v0.1.100'
  echo 'foo "v0.1.101'
  echo 'foo "v0.1.101'
  echo 'foo "v0.1.102'
  echo 'foo "v0.1.102'
  echo 'foo "v0.1.103'
  echo 'foo "v0.1.103'
  echo 'foo "v0.1.104'
  echo 'foo "v0.1.104'
  echo 'foo "v0.1.14'
  echo 'foo "v0.1.14'
  echo 'foo "v0.1.15'
  echo 'foo "v0.1.15'
  echo 'foo "v0.1.16'
  echo 'foo "v0.1.16'
  echo 'foo "v0.1.17'
  echo 'foo "v0.1.17'
  echo 'foo "v0.1.18'
  echo 'foo "v0.1.18'
  echo 'foo "v0.1.19'
  echo 'foo "v0.1.19'
  echo 'foo "v0.1.20'
  echo 'foo "v0.1.20'
  echo 'foo "v0.1.21'
  echo 'foo "v0.1.21'
  echo 'foo "v0.1.22'
  echo 'foo "v0.1.22'
  echo 'foo "v0.1.23'
  echo 'foo "v0.1.23'
  echo 'foo "v0.1.24'
  echo 'foo "v0.1.24'
  echo 'foo "v0.1.25'
  echo 'foo "v0.1.25'
  echo 'foo "v0.1.26'
  echo 'foo "v0.1.26'
  echo 'foo "v0.1.27'
  echo 'foo "v0.1.27'
  echo 'foo "v0.1.28'
  echo 'foo "v0.1.28'
  echo 'foo "v0.1.29'
  echo 'foo "v0.1.29'
  echo 'foo "v0.1.30'
  echo 'foo "v0.1.30'
  echo 'foo "v0.1.31'
  echo 'foo "v0.1.31'
  echo 'foo "v0.1.32'
  echo 'foo "v0.1.32'
  echo 'foo "v0.1.33'
  echo 'foo "v0.1.33'
  echo 'foo "v0.1.90'
  echo 'foo "v0.1.90'
  echo 'foo "v0.1.91'
  echo 'foo "v0.1.91'
  echo 'foo "v0.1.92'
  echo 'foo "v0.1.92'
  echo 'foo "v0.1.93'
  echo 'foo "v0.1.93'
  echo 'foo "v0.1.94'
  echo 'foo "v0.1.94'
  echo 'foo "v0.1.95'
  echo 'foo "v0.1.95'
  echo 'foo "v0.1.96'
  echo 'foo "v0.1.96'
  echo 'foo "v0.1.97'
  echo 'foo "v0.1.97'
  echo 'foo "v0.1.98'
  echo 'foo "v0.1.98'
  echo 'foo "v0.1.99'
  echo 'foo "v0.1.99'
  echo 'foo "v0.10.14'
  echo 'foo "v0.10.14'
  echo 'foo "v0.2.0'
  echo 'foo "v0.2.0'
  echo 'foo "v0.2.1'
  echo 'foo "v0.2.1'
  echo 'foo "v0.2.2'
  echo 'foo "v0.2.2'
  echo 'foo "v0.2.3'
  echo 'foo "v0.2.3'
  echo 'foo "v0.2.4'
  echo 'foo "v0.2.4'
  echo 'foo "v0.2.5'
  echo 'foo "v0.2.5'
  echo 'foo "v0.2.6'
  echo 'foo "v0.2.6'
  echo 'foo "v0.3.0'
  echo 'foo "v0.3.0'
  echo 'foo "v0.3.1'
  echo 'foo "v0.3.1'
  echo 'foo "v0.3.2'
  echo 'foo "v0.3.2'
  echo 'foo "v0.3.3'
  echo 'foo "v0.3.3'
  echo 'foo "v0.3.4'
  echo 'foo "v0.3.4'
  echo 'foo "v0.3.5'
  echo 'foo "v0.3.5'
  echo 'foo "v0.3.6'
  echo 'foo "v0.3.6'
  echo 'foo "v0.3.7'
  echo 'foo "v0.3.7'
  echo 'foo "v0.3.8'
  echo 'foo "v0.3.8'
  echo 'foo "v0.4.0'
  echo 'foo "v0.4.0'
  echo 'foo "v0.4.1'
  echo 'foo "v0.4.1'
  echo 'foo "v0.4.10'
  echo 'foo "v0.4.10'
  echo 'foo "v0.4.11'
  echo 'foo "v0.4.11'
  echo 'foo "v0.4.12'
  echo 'foo "v0.4.12'
  echo 'foo "v0.4.2'
  echo 'foo "v0.4.2'
  echo 'foo "v0.4.3'
  echo 'foo "v0.4.3'
  echo 'foo "v0.4.4'
  echo 'foo "v0.4.4'
  echo 'foo "v0.4.5'
  echo 'foo "v0.4.5'
  echo 'foo "v0.4.6'
  echo 'foo "v0.4.6'
  echo 'foo "v0.4.7'
  echo 'foo "v0.4.7'
  echo 'foo "v0.4.8'
  echo 'foo "v0.4.8'
  echo 'foo "v0.4.9'
  echo 'foo "v0.4.9'
  echo 'foo "v0.5.0'
  echo 'foo "v0.5.0'
  echo 'foo "v0.6.1'
  echo 'foo "v0.6.1'
  echo 'foo "v0.6.10'
  echo 'foo "v0.6.10'
  echo 'foo "v0.6.11'
  echo 'foo "v0.6.11'
  echo 'foo "v0.6.12'
  echo 'foo "v0.6.12'
  echo 'foo "v0.6.13'
  echo 'foo "v0.6.13'
  echo 'foo "v0.6.2'
  echo 'foo "v0.6.2'
  echo 'foo "v0.6.3'
  echo 'foo "v0.6.3'
  echo 'foo "v0.6.4'
  echo 'foo "v0.6.4'
  echo 'foo "v0.6.5'
  echo 'foo "v0.6.5'
  echo 'foo "v0.6.6'
  echo 'foo "v0.6.6'
  echo 'foo "v0.6.7'
  echo 'foo "v0.6.7'
  echo 'foo "v0.6.8'
  echo 'foo "v0.6.8'
  echo 'foo "v0.6.9'
  echo 'foo "v0.6.9'
}

OUTPUT="$(nvm_ls_remote foo)"
EXIT_CODE="$(nvm_ls_remote foo >/dev/null 2>&1 ; echo $?)"
[ "_$OUTPUT" = "_N/A" ] || die "nonexistent version did not report N/A"
[ "_$EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $EXIT_CODE"

OUTPUT="$(nvm_ls_remote)"
EXPECTED_OUTPUT="$(nvm_download | \egrep -o 'v[0-9]+\.[0-9]+\.[0-9]+' | sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n)"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "bare nvm_ls_remote did not output expected sorted versions; got $(echo "$OUTPUT") expected $(echo "$EXPECTED_OUTPUT")"

OUTPUT="$(nvm_ls_remote 0.3)"
EXPECTED_OUTPUT="v0.3.0
v0.3.1
v0.3.2
v0.3.3
v0.3.4
v0.3.5
v0.3.6
v0.3.7
v0.3.8"

[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_ls_remote 0.3 did not output 0.3.x versions; got $OUTPUT"

# Sanity checks
OUTPUT="$(nvm_print_implicit_alias remote stable)"
EXPECTED_OUTPUT="0.10"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_print_implicit_alias remote stable did not output $EXPECTED_OUTPUT; got $OUTPUT"

OUTPUT="$(nvm_print_implicit_alias remote unstable)"
EXPECTED_OUTPUT="0.11"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_print_implicit_alias remote unstable did not output $EXPECTED_OUTPUT; got $OUTPUT"

OUTPUT="$(nvm_ls_remote stable)"
EXPECTED_OUTPUT="v0.10.32"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_ls_remote stable did not output $EXPECTED_OUTPUT; got $OUTPUT"

OUTPUT="$(nvm_ls_remote unstable)"
EXPECTED_OUTPUT="v0.11.14"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_ls_remote unstable did not output $EXPECTED_OUTPUT; got $OUTPUT"

cleanup

